#!/bin/sh

#需要后台启动的脚本
server_name="${0##*/}"
server_name="${server_name%.*}"
server_name="${server_name}.py"

if [ "$1" = "start" ] ; then

	pid=`ps -ef|grep "python -u ${server_name}"| grep -v "grep"|awk '{print $2}'`

	if [ "$pid" != "" ]
	then
			echo "${server_name} already run, stop it first"
			kill -9 ${pid}
	fi

	echo "starting now..."

	nohup python -u ${server_name} > "${server_name}.out" 2>&1 &

	pid=`ps -ef|grep "python -u ${server_name}"| grep -v "grep"|awk '{print $2}'`

	echo ${pid} > "${server_name}.pid"
	echo "${server_name} started at pid: ${pid}"
	echo "${server_name} started at pid: ${pid}" > "${server_name}.out"
elif [ "$1" = "stop" ] ; then
	pid=`ps -ef|grep "python -u ${server_name}"| grep -v "grep"|awk '{print $2}'`
	if [ "$pid" != "" ]
	then
			kill -9 ${pid}
			echo "stop ${server_name} complete"
	else
			echo "${server_name} is not run, there's no need to stop it"
	fi
else
	echo "please enter start or stop!!!"
	exit 1
fi
